#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "debug.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    init();
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::init()
{
    initStyleSheets();
    initMainWidget();
    initBodyLeft();
}

void MainWidget::initStyleSheets()
{
    ui->mainwidget_panel->setStyleSheet(R"(
    #mainwidget_panel {
        background-color: rgb(247, 249, 252);
        border-radius: 12px;
    })");
    ui->mainwidget_body->setStyleSheet(R"(
    #mainwidget_body {
        border-bottom-left-radius: 12px;
        border-bottom-right-radius: 12px;
    })");
    ui->mainwidget_body_left->setStyleSheet(R"(
    #mainwidget_body_left {
        background: qlineargradient(
            x1:0, y1:0.5, x2:0, y2:1,
            stop:0 rgb(240, 243, 246), stop:1 rgb(247, 249, 252)
        );
        border-bottom-left-radius: 12px;
    })");
    ui->mainwidget_body_right->setStyleSheet(R"(
    #mainwidget_body_right {
        border-bottom-right-radius: 12px;
    })");
    ui->mainwidget_head->setStyleSheet(R"(
    #mainwidget_head {
        border-top-left-radius: 12px;
        border-top-right-radius: 12px;
    })");
    ui->mainwidget_head_left->setStyleSheet(R"(
    #mainwidget_head_left {
        background-color: rgb(240, 243, 246);
        border-top-left-radius: 12px;
    })");
    ui->mainwidget_head_right->setStyleSheet(R"(
    #mainwidget_head_right {
        border-top-right-radius: 12px;
    })");
    ui->mainwidget_pushbutton_close->setStyleSheet(R"(
    QPushButton {
        background-repeat: no-repeat;
        background-position: center;
        background-image: url(:/ui/btn_close_50.png);
        border: none;
        background-color: transparent;
    }

    QPushButton:hover,
    QPushButton:pressed {
        background-image: url(:/ui/btn_close_100.png);
    })");
}

# include <QGraphicsDropShadowEffect>
void MainWidget::initMainWidget()
{
    // 顶层(MainWidget)：无边框 + 透明，仅做容器
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 给内容面板(MainWidget_Panel)加阴影
    auto *effect = new QGraphicsDropShadowEffect(ui->mainwidget_panel);
    effect->setColor(Qt::black);
    effect->setOffset(0, 0); // 0偏移
    effect->setBlurRadius(6); // 模糊半径
    ui->mainwidget_panel->setGraphicsEffect(effect);
}

#include <QScrollArea>
#include "mypushbutton.h"
#include "interviewlistwidget.h"
#include "questionlistwidget.h"
#include "companylistwidget.h"
void MainWidget::initBodyLeft()
{
    // 1) 新建一个主布局，设置给 ui->mainwidget_body_left
    QVBoxLayout *mainLayout = new QVBoxLayout(ui->mainwidget_body_left);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 2) 新建 QScrollArea 及其内部容器
    QScrollArea *scrollArea = new QScrollArea(ui->mainwidget_body_left);

    _container  = new QWidget;
    _container ->setStyleSheet(R"( background-color: transparent;)");
    QVBoxLayout *containerLayout = new QVBoxLayout(_container );
    containerLayout->setSpacing(10);
    containerLayout->setContentsMargins(20, 0, 20, 0);
    containerLayout->setAlignment(Qt::AlignTop | Qt::AlignHCenter); // 布局内所有子控件顶中对齐

    // 3) 设置 scrollArea
    scrollArea->setWidget(_container );
    scrollArea->setWidgetResizable(true);
    scrollArea->setStyleSheet(R"( QScrollArea { border: none; background-color: transparent; } QScrollArea > QWidget { background-color: transparent; } QScrollBar:vertical { background-color: transparent; /* 透明背景 */ width: 9px; border: none; } QScrollBar::handle:vertical { background-color: #dcdfe4; border-radius: 4px; min-height: 20px; /* 最小高度 */ } QScrollBar::handle:vertical:hover { background-color: #bec3ca; } QScrollBar::handle:vertical:pressed { background-color: #a8adb5; /* 新增:按下状态 */ } /* 关键:定义滑块上下的空白区域 */ QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: transparent; } QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; /* 隐藏箭头 */ background: transparent; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { background: none; } )");

    // 4) 将 scrollArea 加到主布局里
    mainLayout->addWidget(scrollArea);

    _page_controller = new PageController(ui->mainwidget_body_pages, this);

    auto createBtn = [this](const QString& text, const QString& icon = ":/ui/btn_close_100.png") {
        auto btn = new MyPushButton(ui->mainwidget_body_left);
        QFont font;
        font.setPixelSize(20);
        btn->setFont(font);
        btn->setText("  " + text);
        btn->setIcon(QIcon(icon));
        btn->setFixedHeight(60);
        btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        btn->setStyles(R"(border-radius: 12px; text-align: left; padding-left: 20px;)", R"(background-color: transparent; color: #787F8D;)", R"(background-color: #FC3D4B; color: #ffffff;)");
        return btn;
    };

    auto btn = createBtn("交流");
    _page_controller->addPage(btn, new QWidget(this));
    containerLayout->addWidget(btn);

    btn = createBtn("面经");
    _defaultPageId = _page_controller->addPage(btn, new InterviewListWidget(this));
    containerLayout->addWidget(btn);

    btn = createBtn("题库");
    _page_controller->addPage(btn, new QuestionListWidget(this));
    containerLayout->addWidget(btn);

    btn = createBtn("公司");
    _page_controller->addPage(btn, new CompanyListWidget(this));
    containerLayout->addWidget(btn);

    btn = createBtn("我的面经");
    auto myInterviewPage = new InterviewListWidget(this);
    myInterviewPage->hideRecommendBtn();
    _page_controller->addPage(btn, myInterviewPage);
    containerLayout->addWidget(btn);

#if TEST_UI
    for(int i = 0; i < 100; ++i) {
        QPushButton* widget = new QPushButton(this);
        widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        widget->setStyleSheet(R"(background-color: transparent; color : #000000)");

        auto test_btn = createBtn("测试");
        auto id = _page_controller->addPage(test_btn, widget);
        containerLayout->addWidget(test_btn);

        widget->setText("page id : " + id);
    }
    qDebug() << _defaultPageId;
#endif
    _page_controller->switchToPage(_defaultPageId);
}

#include <QMouseEvent>
void MainWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        _dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        _isDraging = true;
        event->accept();
    }
}

void MainWidget::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && _isDraging) {
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
}

void MainWidget::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    _isDraging = false;
}

void MainWidget::on_mainwidget_pushbutton_close_clicked()
{
    close();
}

#include "uploaddialog.h"
#include "model/datacenter.h"
void MainWidget::on_mainwidget_pushbutton_upload_clicked()
{
    UploadDialog dialog(this);
    if(dialog.exec() == QDialog::Accepted){
#if TEST_UI
        qDebug() << "标题：" << dialog.title();
        qDebug() << "内容：" << dialog.content();
#endif
        auto& dataCenter = model::DataCenter::getInstance();
        model::InterviewInfo info;
        info.title = dialog.title();
        info.content = dialog.content();
        auto myself = dataCenter.getMyself();
        info.sender.userId = myself->userId;
        info.sender.nickname = myself->nickname;
        info.timestamp = time(0);
        dataCenter.uploadInterviewAsync(info);
        connect(&dataCenter, &model::DataCenter::uploadInterviewDone, this, []{
            qDebug() << "上传成功";
        }, Qt::ConnectionType::SingleShotConnection);
    }
}

#include "searchdialog.h"
void MainWidget::on_mainwidget_head_search_returnPressed()
{
    SearchDialog dialog(this);
    dialog.search(ui->mainwidget_head_search->text());
    ui->mainwidget_head_search->setText("");
    dialog.exec();
}
